package gptliaotiantest;

import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class SwingChatClient extends JFrame {
    private JTextArea chatArea;
    private JTextField messageField;
    private JButton sendButton;
    private JTextField ipField;
    private JTextField  nameField;
    private JTextField portField;
    private JButton connectButton;
    private ClientNetworkHandler networkHandler;

    public SwingChatClient() {
        setTitle("Chat Client");
        setSize(400, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        chatArea = new JTextArea();
        chatArea.setEditable(false);
        JScrollPane chatScrollPane = new JScrollPane(chatArea);

        messageField = new JTextField();
        messageField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        });

        sendButton = new JButton("Send");
        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        });

        ipField = new JTextField("111.231.12.66"); // 默认服务器地址
        portField = new JTextField("8080"); // 默认服务器端口
        nameField = new JTextField("b1t"); // 默认服务器端口
        connectButton = new JButton("Connect");
        connectButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    connectToServer();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });

        JPanel inputPanel = new JPanel(new BorderLayout());
        inputPanel.add(ipField, BorderLayout.WEST);
        inputPanel.add(portField, BorderLayout.CENTER);
        inputPanel.add(nameField, BorderLayout.NORTH);
        inputPanel.add(connectButton, BorderLayout.EAST);

        JPanel messagePanel = new JPanel(new BorderLayout());
        messagePanel.add(messageField, BorderLayout.CENTER);
        messagePanel.add(sendButton, BorderLayout.EAST);

        JPanel topPanel = new JPanel(new BorderLayout());
        topPanel.add(inputPanel, BorderLayout.NORTH);
        topPanel.add(messagePanel, BorderLayout.SOUTH);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(chatScrollPane, BorderLayout.CENTER);
        getContentPane().add(topPanel, BorderLayout.NORTH);

        pack();
        setLocationRelativeTo(null);

        this.setSize(400,500);
    }

    private void sendMessage() {
        String message = messageField.getText();
        if (!message.isEmpty()) {
            networkHandler.sendMessage(message);
            messageField.setText("");
        }
    }

    public void exit(){
        this.dispose();
    }

    private void connectToServer() throws IOException {
        String serverIp = ipField.getText();
        int serverPort = Integer.parseInt(portField.getText());
        String name = nameField.getText();

        try {
            networkHandler = new ClientNetworkHandler(this, serverIp, serverPort,name);
            // 禁用连接按钮
            setEnabled(false,connectButton,portField,nameField,ipField);
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }


    }

    private void setEnabled(boolean flag,JComponent ... jComponents){
        for(JComponent j:jComponents){
            j.setEnabled(flag);
        }
    }

    public void appendMessage(String message) {
        chatArea.append(message + "\n");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                SwingChatClient client = new SwingChatClient();
                client.setVisible(true);
            }
        });
    }
}
